package com.izhangqian.plugintest;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;

public class FloatView extends ConstraintLayout {
    private Context mContext;
    private WindowManager mManager;
    public FloatView(@NonNull Context context) {
        this(context, null);
    }

    public FloatView(@NonNull Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public FloatView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        this(context, attrs, defStyleAttr, 0);
    }

    public FloatView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        mManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
        initView();
    }

    private void initView() {
        LayoutInflater.from(mContext).inflate(R.layout.float_view_layout, this);
        setBackgroundColor(getResources().getColor(R.color.white));
    }

    public void showWindow() {
        WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
        layoutParams.flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
        |WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS;
        layoutParams.width = 200;
        layoutParams.height = 300;
        layoutParams.gravity = Gravity.START | Gravity.TOP;
        layoutParams.type = WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY;
        mManager.addView(this, layoutParams);
    }

}
